﻿//
//
//

namespace Microsoft.OpenTurf.Silverlight.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using GalaSoft.MvvmLight.Command;
    using Microsoft.OpenTurf.Silverlight.Common;

    public class LoginViewModel : OpenTurfViewModelBase
    {
        #region Properties
        public string CancelText { get; set; }
        public string OkText { get; set; }
        public string PromptText { get; set; }
        public string PromptTitle { get; set; }
        public Dictionary<Uri, string> OAuthLinks { get; set; }
        #endregion

        #region ctor
        public LoginViewModel()
        {
            // Child Window Size
            SetDefaultWindowSize();

            // Default Text
            SetDefaultFormText();

            // Click Events
            RegisterClickEvents();

            // Login Buttons
            OAuthLinks = new Dictionary<Uri, string>();
            OAuthLinks.Add(new Uri("http://api.twitter.com/oauth/authorize?oauth_token="), "Sign On via Twitter");
        }

        public LoginViewModel(Dictionary<Uri, string> loginLinks)
        {
            // Child Window Size
            SetDefaultWindowSize();

            // Default Text
            SetDefaultFormText();

            // Click Events
            RegisterClickEvents();

            // Login Buttons
            OAuthLinks = loginLinks;
        }
        #endregion

        #region Commands
        public RelayCommand<RoutedEventArgs> LoginClick
        {
            get;
            private set;
        }
        public RelayCommand<RoutedEventArgs> CancelClick
        {
            get;
            private set;
        }
        #endregion

        #region Methods
        private void RegisterClickEvents()
        {
            LoginClick = new RelayCommand<RoutedEventArgs>((e) => HandleLogin(e));
            CancelClick = new RelayCommand<RoutedEventArgs>((e) => HandleCancel(e));
        }
        public void HandleLogin(RoutedEventArgs e)
        {
            Button loginButton = e.OriginalSource as Button;
            ChildWindow loginPrompt = ((Grid)loginButton.Parent).Parent as ChildWindow;

            loginPrompt.DialogResult = false;
        }
        public void HandleCancel(RoutedEventArgs e)
        {
            Button loginButton = e.OriginalSource as Button;
            ChildWindow loginPrompt = ((Grid)loginButton.Parent).Parent as ChildWindow;

            loginPrompt.DialogResult = true;
        }
        private void SetDefaultWindowSize()
        {
            base.SetWindowSize(new GridLength(300), new GridLength(200));
        }
        private void SetDefaultFormText()
        {
            CancelText = "Cancel";
            OkText = "Ok";

            // Child Window Text
            PromptTitle = "Authentication Required";
            PromptText = "Sorry, You must be logged in to do that Action. Please sign in with Twitter.";
        }
        #endregion
    }
}
